<div id="tb<?php echo $uniqid;?>" style="padding:5px">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:;" onclick="categoryAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="categoryEdit();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-bianji"></i><span class="l-btn-text">编辑</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="categoryDelete();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shanchu"></i><span
                    class="l-btn-text">删除</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="categoryReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td>&nbsp;&nbsp;<input id="categoryKeywords" style="width:100px" type="text" class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="categorySearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con"  style="-moz-user-select:none;">
    <table id="category"></table>
</div>
<div id="categoryDlg" class="easyui-dialog" style="width:720px; height:450px;" title="信息框" closed="true"></div>
<script>
    var categoryAdd = function () {
        $("#categoryDlg").dialog({
            title: '添加设备运行类目',
            closed: false,
            width: 720,
            height: 450,
            cache: false,
            href: '/admin/category/add',
            modal: true,
            buttons: [{
                text: '保存',
                id: 'categorysubmit',
                iconCls: 'icon-ok',
            }, {
                text: '关闭',
                iconCls: 'icon-no',
                handler: function () {
                    $("#categoryDlg").dialog('close');
                }
            }]
        });
    };
    var categoryEdit = function (id) {
        let rowId = id || ($('#category').treegrid('getSelected') && $('#category').treegrid('getSelected').id);
        if (rowId) {
            $("#categoryDlg").dialog({
                title: '编辑信息',
                closed: false,
                width: 720,
                height: 450,
                cache: false,
                href: '/admin/category/edit?id=' + rowId,
                modal: true,
                buttons: [{
                    text: '保存',
                    id: 'categorysubmit',
                    iconCls: 'icon-ok',
                }, {
                    text: '关闭',
                    iconCls: 'icon-no',
                    handler: function () {
                        $("#categoryDlg").dialog('close');
                    }
                }]
            });
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
    };
    var categoryDelete = function (id) {
        let rowId = id || ($('#category').treegrid('getSelected') && $('#category').treegrid('getSelected').id);
        if (rowId) {
            $.messager.confirm('提示', '确定删除吗？', function (r) {
                if (r) {
                    $.messager.progress();
                    $.post('/admin/category/delete', {id: rowId}, function (data) {
                        $.messager.progress('close');
                        if (parseInt(data.ret) === 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            categoryReload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.alert('提示', '请先选择要删除的记录！', 'warning');
        }
    };
    var categoryReload = function () {
        $("#category").treegrid('reload');
    };
    var categorySearch = function () {
        var height = $(window).height() - $(".top").height() - 46;
        var width = $(window).width() - $(".leftmenu").width() - 8;
        $("#category").treegrid({
            title: '商品分类',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: true,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 10,
            pageList: [10, 15, 20, 25],
            method: 'post',
            sortName: 'weigh',
            sortOrder: 'desc',
            idField: 'id',
            treeField: 'name',
            url: '/admin/category/index',
            queryParams: {keywords: $('#categoryKeywords').val()},
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: false,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            columns: [[
                {field: 'name', title: '名称', width: fixWidth(20)},
                {field: 'type', title: '栏目类型', width: fixWidth(10)},
                {field: 'image', title: '图片', align:'center', width: fixWidth(7), formatter:function (value){
                        return "<img src='" + value + "' height='35px' style='border-radius: 5px;' />";
                    }},
                {field: 'weigh', title: '排序', width: fixWidth(5)},
                {field: 'status', title: '状态', width: fixWidth(5), formatter:function (value){
                        return value==='normal' ? '显示' : '隐藏';
                    }, styler:function (value) {
                        return value==='normal' ? 'color:green' : 'color:red';
                    }},
                {field: 'description', title: '描述', width: fixWidth(10)},
                {field: 'action', title: '操作', width: fixWidth(13), formatter: function (value, rowData, rowIndex) {
                        return '<a href="javascript:;" class="l-btn l-btn-small" onclick="categoryEdit(' + rowData.id + ')" ><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">编辑</span><span class="l-btn-icon icon-ok">&nbsp;</span></span></a>&nbsp;&nbsp;'+
                            '<a href="javascript:;" class="l-btn l-btn-small" onClick="categoryDelete(' + rowData.id + ')"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">删除</span><span class="l-btn-icon icon-no">&nbsp;</span></span></a>';
                    }
                },
            ]],
            onDblClickRow : function(index,field){
                categoryEdit();
            }
        });
    };
    categorySearch();
    onTreegridResize('#category');
</script>
